package com.hzdl.hotel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzdl.hotel.bean.Administer;
import com.hzdl.hotel.bean.Check;
import com.hzdl.hotel.bean.Customer;
import com.hzdl.hotel.bean.Role;
import com.hzdl.hotel.mapper.AdministerMapper;
import com.hzdl.hotel.service.AdministerService;
import com.hzdl.hotel.vo.AdministerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdministerServiceImpl implements AdministerService {

    @Autowired
    private AdministerMapper am;
    @Override
    public Administer findAdministerByWhere(AdministerVo administerVo) {
        return am.findAdministerByWhere(administerVo);
    }

    @Override
    public List<Check> findCheck() {
        return am.findCheck();
    }

    @Override
    public PageInfo findAdministerByWhere1(AdministerVo administerVo) {
        PageHelper.startPage(administerVo.getPage(),administerVo.getLimit());
        List<Administer> aList = am.findAdministerByWhere1(administerVo);
        PageInfo pageInfo = new PageInfo(aList);
        return pageInfo;
    }

    @Override
    public int addAdminister(AdministerVo administerVo) {
        return am.addAdminister(administerVo);
    }

    @Override
    public int deleteAdminister(Integer aId) {
        return am.deleteAdminister(aId);
    }

    @Override
    public int updateAdminister(AdministerVo administerVo) {
        return am.updateAdminister(administerVo);
    }

    @Override
    public List<Map<String, Object>> findRole(Integer id) {
        //查询所有角色
        List<Role> rList=am.findAllRole();
        //根据admin的id查询admin的职责
        List<Role> arList=am.findRoleById(id);

        List<Map<String,Object>> mList= new ArrayList<>();

        for(Role role:rList){
            Map<String,Object> map = new HashMap<>();
            boolean LAY_CHECKED = false;
            for (Role role1: arList){
                if(role.getrId()== role1.getrId()){
                    LAY_CHECKED = true;
                    break;
                }
            }
            map.put("LAY_CHECKED",LAY_CHECKED);
            map.put("rId",role.getrId());
            map.put("roleName",role.getRoleName());
            map.put("roleDesc",role.getRoleDesc());
            mList.add(map);
        }

        return mList;

    }

    @Override
    public int updateRoleAdmin(AdministerVo administerVo) {
        // 修改用户的角色
        // x先删除当前用户所有的角色
        int row = am.deleteRoleByAId(administerVo.getId());
        // 添加新角色
        row += am.addRoleAdmin(administerVo);

        return row;
    }

    @Override
    public int deleteRoleByAId(Integer id) {
        return am.deleteRoleByAId(id);
    }


}
